package com.example.hunheandroid


import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import com.example.hunheandroid.databinding.ActivityGoFlutterBinding
import com.example.hunheandroid.databinding.ActivityStudyChannelBinding
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.BasicMessageChannel
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodCodec
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.common.StandardMethodCodec
import io.flutter.plugin.common.StringCodec
import io.flutter.plugins.BasePlugin

import io.flutter.plugins.HostMessageApi


class StudyChannelActivity : AppCompatActivity() {
    private val BASIC_CHANNEL = "study_channel"

    private lateinit var basicMessageChannel: BasicMessageChannel<String?>
    private lateinit var methodChannel: MethodChannel
    private lateinit var eventChannel: EventChannel
    private var eventSink: EventChannel.EventSink? = null
    private lateinit var binding: ActivityStudyChannelBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityStudyChannelBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val flutterFragment = FlutterFragment.withCachedEngine(ENGINE_ID).build<FlutterFragment>()

        supportFragmentManager.beginTransaction().add(binding.flutterViewRoot.id, flutterFragment)
            .commit()

        configureFlutterEngine(FlutterLibrary.getEngine()!!)

    }


    private fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        Log.e("StudyChannelActivity", "configureFlutterEngine")
        basicMessageChannel = BasicMessageChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            BASIC_CHANNEL,
            StringCodec.INSTANCE
        )
        //创建MethodChannel
        methodChannel =
            MethodChannel(flutterEngine.dartExecutor, BASIC_CHANNEL, StandardMethodCodec.INSTANCE)

        //创建EventChannel
        eventChannel = EventChannel(flutterEngine.dartExecutor, BASIC_CHANNEL)


        basicMessageChannel.send("dddd")
        basicMessageChannel.setMessageHandler { message, reply ->
            reply.reply("dddd")
            Log.e("dddd", message.toString())
        }

        methodChannel.invokeMethod("getPlatformVersion", null)
        methodChannel.setMethodCallHandler { call, result ->
            when (call.method) {
                "getPlatformVersion" -> {
                    result.success("Android ${android.os.Build.VERSION.RELEASE}")
                }

                else -> {
                    result.notImplemented()
                }
            }
        }

        eventChannel.setStreamHandler(object : EventChannel.StreamHandler {
            override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                eventSink = events
                eventSink?.success("Event")
            }

            override fun onCancel(arguments: Any?) {
                eventSink = null
            }
        })

    }


}